package com.brush.pig.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brush.common.constant.CacheConstants;
import com.brush.common.exception.database.PrimaryKeyException;
import com.brush.common.exception.database.RecordNotFoundException;
import com.brush.pig.domain.IotPigTransfer;
import com.brush.pig.domain.req.pig.PigTransferAddReq;
import com.brush.pig.domain.req.pig.PigTransferEditReq;
import com.brush.pig.domain.req.pig.PigTransferQueryReq;
import com.brush.pig.domain.resp.pig.PigTransferResp;
import com.brush.pig.service.IotPigTransferService;
import com.brush.pig.mapper.IotPigTransferMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author ckq
* @description 针对表【iot_pig_transfer(猪只转舍)】的数据库操作Service实现
* @createDate 2024-11-13 09:03:42
*/
@Service
public class IotPigTransferServiceImpl extends ServiceImpl<IotPigTransferMapper, IotPigTransfer>
    implements IotPigTransferService{

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageInfo<PigTransferResp> paginationQuery(PigTransferQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean add(PigTransferAddReq req) {
        //判断编号是否重复
        if(this.baseMapper.exists(new LambdaQueryWrapper<IotPigTransfer>().eq(IotPigTransfer::getCode, req.getCode()))) {
            throw new RuntimeException("编号重复");
        }
        IotPigTransfer db = new IotPigTransfer();
        BeanUtils.copyProperties(req, db);
        boolean result = this.save(db);
        if (result) {
            redisTemplate.opsForValue().set(CacheConstants.PIG_KEY+db.getCode(), db);
        }
        return result;
    }

    @Override
    public PigTransferResp findById(Long id) {
        //验证主键是否存在
        if (Objects.isNull(id)) {
            throw new PrimaryKeyException();
        }
        //根据id查询猪转舍的信息
        IotPigTransfer db = this.getById(id);
        //数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RuntimeException("数据不存在");
        }
        //封装成返回对象
        PigTransferResp resp = new PigTransferResp();
        BeanUtils.copyProperties(db, resp);

        return resp;
    }

    @Override
    public boolean edit(PigTransferEditReq req) {
        //根据主键查询
        IotPigTransfer db = this.getById(req.getId());
        //数据库找不到数据
        if (Objects.isNull(db)) {
            throw new RecordNotFoundException();
        }

        //覆盖数据库对象的值
        BeanUtils.copyProperties(req, db);
        boolean result = this.updateById(db);
        if (result) {
            //清除缓存
            redisTemplate.opsForValue().set(CacheConstants.PIG_KEY+db.getCode(), db);
        }
        return result;
    }
}




